using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.RoadData.Input;
//using HuiLian.Service.Platform.RoadData.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;

namespace HuiLian.Service.Platform.RoadData
{
    /// <summary>
    /// 道路中心线服务
    /// </summary>
    public class RoadDataService : IRoadDataService
    {
        private readonly IMapper _mapper;
        private readonly IRoadDataRepository _roadDataRepository;

        public RoadDataService(IMapper mapper, IRoadDataRepository roadDataRepository)
        {
            _mapper = mapper;
            _roadDataRepository = roadDataRepository;
        }

        /// <summary>
        /// 获取道路中心线信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _roadDataRepository.GetAsync(id);
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询道路中心线信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<RoadDataEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _roadDataRepository.Select
            //.WhereDynamicFilter(input.GetDynamicFilter<DynamicFilterInfo>())
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync();

            var data = new PageOutput<RoadDataEntity>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加道路中心线信息
        /// </summary>
        /// <param name="input">道路中心线信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(RoadDataAddInput input)
        {
            var entity = _mapper.Map<RoadDataEntity>(input);
            var id = (await _roadDataRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新道路中心线信息
        /// </summary>
        /// <param name="input">道路中心线信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(RoadDataUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _roadDataRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _roadDataRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除道路中心线信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _roadDataRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除道路中心线信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _roadDataRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除道路中心线信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _roadDataRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 添加列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddListAsync(List<RoadDataEntity> list)
        {
            var entitys = await _roadDataRepository.DeleteAsync(p => true);
            var count = (await _roadDataRepository.InsertAsync(list)).Count;
            return ResponseOutput.Result(count > 0);
        }

    }
}
